package com.bookstore.controller;

import com.bookstore.common.Result;
import com.bookstore.entity.Category;
import com.bookstore.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/category")
@Api(tags = "分类管理接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    @ApiOperation("添加分类")
    public Result<Boolean> addCategory(@RequestBody Category category) {
        return Result.success(categoryService.addCategory(category));
    }

    @PutMapping
    @ApiOperation("更新分类")
    public Result<Boolean> updateCategory(@RequestBody Category category) {
        return Result.success(categoryService.updateCategory(category));
    }

    @DeleteMapping("/{categoryId}")
    @ApiOperation("删除分类")
    public Result<Boolean> deleteCategory(
            @ApiParam("分类ID") @PathVariable Integer categoryId) {
        return Result.success(categoryService.deleteCategory(categoryId));
    }

    @GetMapping("/list")
    @ApiOperation("获取分类列表")
    public Result<List<Category>> getCategoryList() {
        return Result.success(categoryService.getCategoryList());
    }

    @GetMapping("/sub/{parentId}")
    @ApiOperation("获取子分类列表")
    public Result<List<Category>> getSubCategoryList(
            @ApiParam("父分类ID") @PathVariable Integer parentId) {
        return Result.success(categoryService.getSubCategoryList(parentId));
    }

    @GetMapping("/tree")
    @ApiOperation("获取分类树形结构")
    public Result<List<Category>> getCategoryTree() {
        return Result.success(categoryService.getCategoryTree());
    }
} 